package inits

import (
	"fmt"
	"fresh-e-commerce/config"
	"fresh-e-commerce/global"
	"fresh-e-commerce/models"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitMysql() {
	var err error
	con := config.NaCos.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local",
		con.User, con.Password, con.Host, con.Port, con.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql连接成功")

	err = global.DB.Debug().AutoMigrate(&models.User{}, &models.Order{}, &models.Product{}, &models.Attributes{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据迁移成功")
}
